package com.example.e_comm.service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.mapper.RoleMapper;
import com.example.e_comm.mapper.RolePermissionMapper;
import com.example.e_comm.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.entity.Role;
import com.example.e_comm.entity.RolePermission;
import com.example.e_comm.service.RoleService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 迪斯乔斯达
 * @version 0.1
 * @data 2025/10/17 10:29
 */
@Service
public class RoleServiceImpl
        extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    private final RoleMapper mapper;

    private final PermissionService permissionService;

    private final RolePermissionMapper rolePermissionMapper;

    public RoleServiceImpl(RoleMapper mapper, PermissionService permissionService, RolePermissionMapper rolePermissionMapper) {
        this.mapper = mapper;
        this.permissionService = permissionService;
        this.rolePermissionMapper = rolePermissionMapper;
    }

    @Override
    public void assignPermissions(Long roleId, List<Long> pIds) {
        // 清空角色原有权限关系 role_permission 数据表
        rolePermissionMapper.delete(new QueryWrapper<RolePermission>().eq("role_id", roleId));

        // 为角色分配新的权限
        if (pIds != null && !pIds.isEmpty()) {
            List<RolePermission> rps = pIds.stream()
                    .map(pId -> {
                        RolePermission rp = new RolePermission();
                        rp.setRoleId(roleId);
                        rp.setPermissionId(pId);
                        return rp;
                    }).collect(Collectors.toList());
            for(RolePermission rp : rps) {
                rolePermissionMapper.insert(rp);
            }
        }
    }

    @Override
    public List<Role> getRolesByUserId(Long userId) {
        return mapper.getRolesByUserId(userId);
    }

    @Override
    public Role getRoleWithPermissions(Long roleId) {
        // 获取角色详情
        Role role = this.getById(roleId);
        // 查询角色的权限列表
        if (role != null) {
            List<Permission> permissions = permissionService.getPermissionsByRoleId(roleId);
            role.setPermissions(permissions);
        }
        return role;
    }

    @Override
    public Optional<Object> search(Page<Role> page, LambdaQueryWrapper<Role> like) {
        return Optional.empty();
    }

    @Override
    public boolean saveBatch(Collection<Role> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Role> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Role> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Role entity) {
        return false;
    }

    @Override
    public Role getOne(Wrapper<Role> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<Role> getOneOpt(Wrapper<Role> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Role> queryWrapper) {
        return Map.of();
    }

    @Override
    public <V> V getObj(Wrapper<Role> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

}

